﻿using Autofac;
using Autofac.Core;
using Autofac.Integration.WebApi;
using Hrm.Core;
using Hrm.EntityFramework;
using Hrm.Services;
using System;
using System.Collections.Generic;
using System.Data.Entity.Core.Objects;
using System.Data.Entity.Infrastructure;
using System.Diagnostics;
using System.Linq;
using System.Reflection;
using System.Transactions;
using System.Web;
using System.Web.Http;

namespace Hrm.Api
{
    public class Bootstraper
    {
        public static void Run()
        {
            var builder = new ContainerBuilder();

            builder.RegisterType<AppDomainTypeFinder>().As<ITypeFinder>().SingleInstance();
            //builder.Register<IDbContext>(c => new HrmDbContext("name=connectionStrings")).InstancePerLifetimeScope();
            //builder.RegisterGeneric(typeof(EfRepository<>)).As(typeof(IRepository<>)).InstancePerLifetimeScope();
            builder.RegisterType<HrmDbContext>().As<IDbContext>().Named<IDbContext>("HrmDb").SingleInstance();
            builder.RegisterType<PermissionContext>().As<IDbContext>().Named<IDbContext>("Permission").SingleInstance();

            builder.RegisterAssemblyTypes(AppDomain.CurrentDomain.GetAssemblies())
                .Where(t => t.Name.EndsWith("HrmDbRepository")).WithParameter(ResolvedParameter.ForNamed<IDbContext>("HrmDb"))
                .AsImplementedInterfaces();

            builder.RegisterAssemblyTypes(AppDomain.CurrentDomain.GetAssemblies())
                .Where(t => t.Name.EndsWith("PermissionDbRepository")).WithParameter(ResolvedParameter.ForNamed<IDbContext>("Permission"))
                .AsImplementedInterfaces();

            builder.RegisterAssemblyTypes(AppDomain.CurrentDomain.GetAssemblies())
                .Where(t => t.Name.EndsWith("Service"))
                .AsImplementedInterfaces();

            builder.RegisterApiControllers(Assembly.GetExecutingAssembly());
            var container = builder.Build();
            var resolver = new AutofacWebApiDependencyResolver(container);
            GlobalConfiguration.Configuration.DependencyResolver = resolver;

            //ServiceModule.Initialize();

            //TransactionOptions transactionOption = new TransactionOptions();
            //transactionOption.IsolationLevel = IsolationLevel.ReadCommitted;
            //transactionOption.Timeout = new TimeSpan(0, 0, 60);
            //using (TransactionScope transaction = new TransactionScope(TransactionScopeOption.Required, transactionOption))
            //{
            //    PermissionContext context1 = (PermissionContext)container.ResolveNamed<IDbContext>("Permission");
            //    HrmDbContext context2 = (HrmDbContext)container.ResolveNamed<IDbContext>("HrmDb");
            //    context1.Database.Log = p => Debug.WriteLine(p);
            //    context2.Database.Log = p => Debug.WriteLine(p);
            //    IUserService userService = container.Resolve<IUserService>();
            //    IRoleService roleService = container.Resolve<IRoleService>();
            //    var role = new Role()
            //    {
            //        RoleName = "RoleName",
            //    };
            //    var roleIndex = roleService.AddRole(role);
            //    var user = new User()
            //    {
            //        UserName = "UserName",
            //        Telephone = "Telephone",
            //        Address = "Address",
            //        DepartmentId = 1
            //    };
            //    var userIndex = userService.AddPerson(user);
            //    transaction.Complete();
            //}

        }
    }
}